package exia.java.core.services;


import java.io.Serializable;
import java.net.InetAddress;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class RemoteServiceFactoryServer extends UnicastRemoteObject implements IRemoteServiceFactory, Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static RemoteServiceFactory remoteServiceFactory;

	public RemoteServiceFactoryServer()throws RemoteException{
		try{
			Naming.rebind("rmi://"+InetAddress.getLocalHost().getHostName()+"/RemoteServiceFactoryServer", this);
			System.out.println("RemoteMethodsServer is bound in registry (address: rmi://"+InetAddress.getLocalHost().getHostName()+"/RemoteMethodsServer)");
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	@Override public RemoteServiceFactory getServiceFactory() throws RemoteException {
		if(remoteServiceFactory == null){
			remoteServiceFactory = new RemoteServiceFactory();
		}
		return remoteServiceFactory;
	}
	
	public static void main(final String[] args){
		try{
			if(System.getSecurityManager() == null){
				System.setSecurityManager(new SecurityManager());
			}
			RemoteServiceFactoryServer server = new RemoteServiceFactoryServer();
			Registry registry = LocateRegistry.getRegistry();
			registry.rebind("rmi://"+InetAddress.getLocalHost().getHostName()+":1099/RemoteServiceFactoryServer_ExiaBookshop", server);
			System.out.println("RemoteMethodsServer is bound in registry (address: rmi://"+InetAddress.getLocalHost().getHostName()+"/RemoteServiceFactoryServer_ExiaBookshop)");
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}
